package com.erpanderp.wam.usm.model.service.user;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.usm.model.entity.complaintandsuggestion.ComplaintAndSuggestion;
import com.erpanderp.wam.util.HibernateUtil;

public class ComplaintsAndSuggestionDAO {

	
	public ComplaintAndSuggestion createComplaintAndSuggestion(ComplaintAndSuggestion complaintsAndSuggestion){
		try{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		System.out.println("FLUSH MODE: " + session.getFlushMode());
		session.setFlushMode(FlushMode.COMMIT);
		session.saveOrUpdate(complaintsAndSuggestion);
		session.getTransaction().commit();
		return complaintsAndSuggestion;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
  public String getLastGeneratedComplaintCode(){
		
		String queryString="select c.code from ComplaintAndSuggestion c ";
		
		System.out.println("QUERY: " + queryString);
		
		List<String> codeList = null;
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();		
			Query query = session.createQuery(queryString);
			query.setCacheable(false);
			codeList = (List<String>) query.list();
			session.getTransaction().commit();
		}catch(HibernateException e){
			e.printStackTrace();
		}
		
		String lastCode = null;
		for (String code : codeList) {
			
			if (lastCode == null) {
				lastCode = code;
				continue;
				
			}else if (Long.parseLong(code.substring(2)) > Long.parseLong(lastCode.substring(2))) {
					lastCode = code;
					continue;
				
			}
			System.out.println(lastCode + "============ " + code);
		}		
		return lastCode;	
	}
  
  public boolean isComplaintCodeExistOrNot(String complaintCode) {
		boolean isComplaintExist = false;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();
			String queryString = "from ComplaintAndSuggestion c where c.code=:complaintCode";
			Query query = session.createQuery(queryString).setString(
					"complaintCode", complaintCode);
			query.setCacheable(false);

			List tempList = query.list();

			if (tempList == null || tempList.size() == 0) {
				isComplaintExist = true;
			}

			session.close();
			return isComplaintExist;
		} catch (Exception e) {
			e.printStackTrace();
			return isComplaintExist;
		}
	}
  
  public List<ComplaintAndSuggestion> getAllCompliantsAndSuggestions()
	{
		List<ComplaintAndSuggestion> complaintsAndSuggestions = null;
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();			
			session.beginTransaction();
			
			String queryString = "from ComplaintAndSuggestion c order by c.dateRaised desc";
			
			Query query = session.createQuery(queryString);
			query.setCacheable(false);
			complaintsAndSuggestions = (List<ComplaintAndSuggestion>) query.list();	
			
			session.close();
			return complaintsAndSuggestions;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}
  
  public ComplaintAndSuggestion getComplaintAndSuggestionByCode(String complaintCode){
	  ComplaintAndSuggestion complaintAndSuggestion;
	 	try{
	 		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	 		session.beginTransaction();
	 		session.setFlushMode(FlushMode.AUTO);
	 		String queryString = "from ComplaintAndSuggestion cs where cs.code =:complaintCode";
	 		Query query = session.createQuery(queryString).setString("complaintCode", complaintCode);
	 		query.setCacheable(false);
	 		List<ComplaintAndSuggestion> tempList = query.list();
			session.getTransaction().commit();
			
			if(tempList != null && tempList.size()>0){
				complaintAndSuggestion = tempList.get(0);
				return complaintAndSuggestion;
			}else{
				return null;
			} 		
	 		
	 	}catch (Exception e) {
	 		e.printStackTrace();
	 		return null;			
		}
	 	
	 }
	

}
